package com.example.ymm.modules1.service.dd.ms;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.example.ymm.modules1.entity.my.ms.MsMouldMain;
import com.example.ymm.modules1.entity.my.sys.SysApprovalSetting;
import com.example.ymm.modules1.entity.my.user.*;
import com.example.ymm.modules1.query.DbOrderItemQueryCriteria;
import com.example.ymm.modules1.service.dto.heng.ApproverMsgNotifyDto;
import com.example.ymm.modules1.service.dto.heng.ImportXftDataDto;
import com.example.ymm.modules1.service.dto.heng.SubmitXftDataDto;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.ServletUtils;
import com.example.ymm.util.UtilValidate;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DingNoticeService {

    /**
     * 二次开发系统通知
     */

    @Value("${dd_h5.agentId}")
    private Long agentId;

    @Value("${dd_h5.appId}")
    String appId;

    @Value("${dd_h5.corpid}")
    String corpid;

    @Value("${oa_app.agentId}")
    private Long oa_agentId;

    @Value("${oa_app.appId}")
    String oa_appId;

    @Value("${oa_app.corpid}")
    String oa_corpid;


    @Autowired
    AccessTokenH5Service2 accessTokenH5Service;

    @Autowired
    AccessTokenOaAppService accessTokenOaAppService;

    /**
     * 模具费用待办通知
     */
    public void OATempleNotice(MsMouldMain mouldMain)  {
        if(UtilValidate.isEmpty(mouldMain)){
            return;
        }
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        request.setUseridList(mouldMain.getUserId());
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();

        OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
        head.setBgcolor("FFFF6A00");
        head.setText("请尽快处理");
        OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
        List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<>();
        OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form1.setKey("金额:");
        form1.setValue(mouldMain.getPrice().doubleValue()+"");
        OapiMessageCorpconversationAsyncsendV2Request.Form form2 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form2.setKey("开始时间:");
        form2.setValue(mouldMain.getStartDate());
        OapiMessageCorpconversationAsyncsendV2Request.Form form3 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form3.setKey("经办人:");
        form3.setValue(mouldMain.getUserName());

//        OapiMessageCorpconversationAsyncsendV2Request.Rich rich = new OapiMessageCorpconversationAsyncsendV2Request.Rich();
//        rich.setNum("200");
//        rich.setUnit("元");

        list.add(form1);
        list.add(form2);
        list.add(form3);


        body.setForm(list);
//        body.setRich(rich);
        body.setTitle("模具费用退款待确认通知");
//        body.setImage("@lADOADmaWMzazQKA");
//        body.setFileCount("3");
//        body.setAuthor("小钉");


        OapiMessageCorpconversationAsyncsendV2Request.StatusBar statusBar = new OapiMessageCorpconversationAsyncsendV2Request.StatusBar();
        statusBar.setStatusBg("0xFFFF6A00");
        statusBar.setStatusValue("进行中");
        String url="http://220.161.77.66:10111/#/h5Login?redirect=/ms/mouldList";
        String encode= ServletUtils.urlEncode(url);
        String message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+appId+"&redirect_type=jump&redirect_url="+encode;//&redirect_url=/h5Login
        String message_url3="dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+agentId+"&redirect_type=jump&redirect_url=h5Login";

        String message_url2="dingtalk://dingtalkclient/page/link?url="+encode+"&pc_slide=true";
        //http://220.161.77.66:10111/#/h5Login
        oa.setMessageUrl(message_url);
        oa.setHead(head);
        oa.setBody(body);
        oa.setStatusBar(statusBar);
        msg.setOa(oa);
        msg.setMsgtype("oa");
        msg.setOa(oa);
        request.setMsg(msg);

        String accessToken = accessTokenH5Service.getAccessToken();
        try{
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }

    /**
     * 待审批通知
     */
    public void approvaWaitNotice(DbUserWait wait,SysApprovalSetting approvalSetting)  {
        if(UtilValidate.isEmpty(wait)){
            return;
        }
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        request.setUseridList(wait.getUserId());
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();

        OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
        head.setBgcolor("FFFF6A00");
        head.setText("请尽快处理");
        OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
        List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<>();
        OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form1.setKey("审批编号:");
        form1.setValue(wait.getApproveNo());
        OapiMessageCorpconversationAsyncsendV2Request.Form form2 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form2.setKey("审批类型:");
        form2.setValue(approvalSetting.getApprovalName());
        OapiMessageCorpconversationAsyncsendV2Request.Form form3 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form3.setKey("发起人:");
        form3.setValue(wait.getCreateUserName());

//        OapiMessageCorpconversationAsyncsendV2Request.Rich rich = new OapiMessageCorpconversationAsyncsendV2Request.Rich();
//        rich.setNum("200");
//        rich.setUnit("元");

        list.add(form1);
        list.add(form2);
        list.add(form3);


        body.setForm(list);
//        body.setRich(rich);
        body.setTitle(wait.getTitle());
//        body.setImage("@lADOADmaWMzazQKA");
//        body.setFileCount("3");
//        body.setAuthor("小钉");


        OapiMessageCorpconversationAsyncsendV2Request.StatusBar statusBar = new OapiMessageCorpconversationAsyncsendV2Request.StatusBar();
        statusBar.setStatusBg("0xFFFF6A00");
        statusBar.setStatusValue("进行中");
        String url="http://220.161.77.66:10111/#/h5Login?redirect="+approvalSetting.getPage();
        String encode= ServletUtils.urlEncode(url);
        String message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+appId+"&redirect_type=jump&redirect_url="+encode;//&redirect_url=/h5Login
        String accessToken = accessTokenH5Service.getAccessToken();

        if(approvalSetting.getApprovalSettingId().equals("3")){
            accessToken =accessTokenOaAppService.getAccessToken();
            url="http://220.161.77.66:10111/oa_app/#/pages/kq/approve/list";
            encode= ServletUtils.urlEncode(url);
            message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+oa_appId+"&redirect_type=jump&redirect_url="+encode;
        }
        //http://220.161.77.66:10111/#/h5Login
        oa.setMessageUrl(message_url);
        oa.setHead(head);
        oa.setBody(body);
        oa.setStatusBar(statusBar);
        msg.setOa(oa);
        msg.setMsgtype("oa");
        msg.setOa(oa);
        request.setMsg(msg);

        try{
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }


    /**
     * 参与人 通知
     */
    public void approvaJoinNotice(ApproverMsgNotifyDto dto)  {
        if(UtilValidate.isEmpty(dto)){
            return;
        }
        SysApprovalSetting approvalNameSetting = dto.getApprovalNameSetting();

        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        request.setUseridList(dto.getUserId());
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();

        OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
        head.setBgcolor("9999FF");
        head.setText("请知悉");
        OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
        List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<>();
        OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form1.setKey("审批编号:");
        form1.setValue(dto.getApproveNo());
        OapiMessageCorpconversationAsyncsendV2Request.Form form2 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form2.setKey("审批名称:");
        form2.setValue(approvalNameSetting.getApprovalName());
        OapiMessageCorpconversationAsyncsendV2Request.Form form3 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form3.setKey("发起人:");
        form3.setValue(dto.getCreateUserName());

//        OapiMessageCorpconversationAsyncsendV2Request.Rich rich = new OapiMessageCorpconversationAsyncsendV2Request.Rich();
//        rich.setNum("200");
//        rich.setUnit("元");

        list.add(form1);
        list.add(form2);
        list.add(form3);


        body.setForm(list);
//        body.setRich(rich);
        body.setTitle(dto.getCreateUserName()+"《"+approvalNameSetting.getApprovalName()+"》审批已通过");
//        body.setImage("@lADOADmaWMzazQKA");
//        body.setFileCount("3");
//        body.setAuthor("小钉");


        OapiMessageCorpconversationAsyncsendV2Request.StatusBar statusBar = new OapiMessageCorpconversationAsyncsendV2Request.StatusBar();
        statusBar.setStatusBg("0xFFFF6A00");
        statusBar.setStatusValue("进行中");
        String url="http://220.161.77.66:10111/#/h5Login?redirect="+approvalNameSetting.getPage();
        String encode= ServletUtils.urlEncode(url);
        String message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+appId+"&redirect_type=jump&redirect_url="+encode;//&redirect_url=/h5Login
        String accessToken = accessTokenH5Service.getAccessToken();
        if(approvalNameSetting.getApprovalSettingId().equals("3")){
            accessToken=accessTokenOaAppService.getAccessToken();
            url="http://220.161.77.66:10111/oa_app/#/pages/kq/approve/list";
            encode= ServletUtils.urlEncode(url);
            message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+oa_appId+"&redirect_type=jump&redirect_url="+encode;
        }
        //http://220.161.77.66:10111/#/h5Login
        oa.setMessageUrl(message_url);
        oa.setHead(head);
        oa.setBody(body);
        oa.setStatusBar(statusBar);
        msg.setOa(oa);
        msg.setMsgtype("oa");
        msg.setOa(oa);
        request.setMsg(msg);


        try{
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }


    /**
     * 审批驳回
     */
    public void approvaBackNotice(DbApprovalLog approvalLog, String comment, String userName, SysApprovalSetting approvalNameSetting)  {

        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        request.setUseridList(approvalLog.getUserId());
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();

        OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
        head.setBgcolor("FF0000");
        head.setText("点击查看");
        OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
        List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<>();
        OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form1.setKey("审批编号:");
        form1.setValue(approvalLog.getApproveNo());
        OapiMessageCorpconversationAsyncsendV2Request.Form form2 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form2.setKey("驳回原因:");
        form2.setValue(comment);
        OapiMessageCorpconversationAsyncsendV2Request.Form form3 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form3.setKey("审批人:");
        form3.setValue(userName);

//        OapiMessageCorpconversationAsyncsendV2Request.Rich rich = new OapiMessageCorpconversationAsyncsendV2Request.Rich();
//        rich.setNum("200");
//        rich.setUnit("元");

        list.add(form1);
        list.add(form2);
        list.add(form3);


        body.setForm(list);
//        body.setRich(rich);
        body.setTitle("您的"+approvalNameSetting.getApprovalName()+"被驳回");
//        body.setImage("@lADOADmaWMzazQKA");
//        body.setFileCount("3");
//        body.setAuthor("小钉");


        OapiMessageCorpconversationAsyncsendV2Request.StatusBar statusBar = new OapiMessageCorpconversationAsyncsendV2Request.StatusBar();
        statusBar.setStatusBg("0xFF0000");
        statusBar.setStatusValue("驳回");
        String url="http://220.161.77.66:10111/#/h5Login?redirect="+approvalNameSetting.getPage();
        String encode= ServletUtils.urlEncode(url);
        String message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+appId+"&redirect_type=jump&redirect_url="+encode;//&redirect_url=/h5Login
        String accessToken = accessTokenH5Service.getAccessToken();
        if(approvalNameSetting.getApprovalSettingId().equals("3")){
            accessToken=accessTokenOaAppService.getAccessToken();
            url="http://220.161.77.66:10111/oa_app/#/pages/kq/approve/list";
            encode= ServletUtils.urlEncode(url);
            message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+oa_appId+"&redirect_type=jump&redirect_url="+encode;
        }
        //http://220.161.77.66:10111/#/h5Login
        oa.setMessageUrl(message_url);
        oa.setHead(head);
        oa.setBody(body);
        oa.setStatusBar(statusBar);
        msg.setOa(oa);
        msg.setMsgtype("oa");
        msg.setOa(oa);
        request.setMsg(msg);


        try{
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }


    /**
     * 审批撤回
     */
    public void approvalCancelNotice(ApproverMsgNotifyDto dto){
        if(UtilValidate.isEmpty(dto)){
            return;
        }
        SysApprovalSetting approvalNameSetting = dto.getApprovalNameSetting();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        request.setUseridList(dto.getUserId());
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();

        OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
        head.setBgcolor("9999FF");
        head.setText("请知悉");
        OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
        List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<>();
        OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form1.setKey("审批编号:");
        form1.setValue(dto.getApproveNo());
        OapiMessageCorpconversationAsyncsendV2Request.Form form2 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form2.setKey("审批名称:");
        form2.setValue(dto.getApproverName());
        OapiMessageCorpconversationAsyncsendV2Request.Form form3 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form3.setKey("发起人:");
        form3.setValue(dto.getCreateUserName());

//        OapiMessageCorpconversationAsyncsendV2Request.Rich rich = new OapiMessageCorpconversationAsyncsendV2Request.Rich();
//        rich.setNum("200");
//        rich.setUnit("元");

        list.add(form1);
        list.add(form2);
        list.add(form3);


        body.setForm(list);
//        body.setRich(rich);
        body.setTitle(dto.getCreateUserName()+"撤回了《"+dto.getApproverName()+"》");
//        body.setImage("@lADOADmaWMzazQKA");
//        body.setFileCount("3");
//        body.setAuthor("小钉");


        OapiMessageCorpconversationAsyncsendV2Request.StatusBar statusBar = new OapiMessageCorpconversationAsyncsendV2Request.StatusBar();
        statusBar.setStatusBg("0xFFFF6A00");
        statusBar.setStatusValue("已撤回");
        String url="http://220.161.77.66:10111/#/h5Login?redirect="+approvalNameSetting.getPage();
        String encode= ServletUtils.urlEncode(url);
        String message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+appId+"&redirect_type=jump&redirect_url="+encode;//&redirect_url=/h5Login
        String accessToken = accessTokenH5Service.getAccessToken();
        if(approvalNameSetting.getApprovalSettingId().equals("3")){
            accessToken=accessTokenOaAppService.getAccessToken();
            url="http://220.161.77.66:10111/oa_app/#/pages/kq/approve/list";
            encode= ServletUtils.urlEncode(url);
            message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+oa_appId+"&redirect_type=jump&redirect_url="+encode;
        }
        //http://220.161.77.66:10111/#/h5Login
        oa.setMessageUrl(message_url);
        oa.setHead(head);
        oa.setBody(body);
        oa.setStatusBar(statusBar);
        msg.setOa(oa);
        msg.setMsgtype("oa");
        msg.setOa(oa);
        request.setMsg(msg);


        try{
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }


    /**
     * 审批全部通过
     */
    public void approvaAllPassNotice(DbApprovalLog approvalLog,String comment,SysApprovalSetting approvalSetting)  {
        if(UtilValidate.isEmpty(approvalLog)){
            return;
        }
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        request.setUseridList(approvalLog.getUserId());
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();

        OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
        head.setBgcolor("00FFFF");
        head.setText("点击查看");
        OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
        List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<>();
        OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form1.setKey("审批编号:");
        form1.setValue(approvalLog.getApproveNo());
        OapiMessageCorpconversationAsyncsendV2Request.Form form2 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form2.setKey("已审批完成:");
        form2.setValue(comment);

        list.add(form1);
        list.add(form2);
//        list.add(form3);


        body.setForm(list);
//        body.setRich(rich);
        body.setTitle("您的"+approvalSetting.getApprovalName()+"已通过");
//        body.setImage("@lADOADmaWMzazQKA");
//        body.setFileCount("3");
//        body.setAuthor("小钉");


        OapiMessageCorpconversationAsyncsendV2Request.StatusBar statusBar = new OapiMessageCorpconversationAsyncsendV2Request.StatusBar();
        statusBar.setStatusBg("0x00FF00");
        statusBar.setStatusValue("审批完成");
        String url="http://220.161.77.66:10111/#/h5Login?redirect="+approvalSetting.getPage();
        String encode= ServletUtils.urlEncode(url);
        String message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+appId+"&redirect_type=jump&redirect_url="+encode;//&redirect_url=/h5Login
        String accessToken = accessTokenH5Service.getAccessToken();

        if(approvalSetting.getApprovalSettingId().equals("3")){
            accessToken=accessTokenOaAppService.getAccessToken();
            url="http://220.161.77.66:10111/oa_app/#/pages/kq/approve/list";
            encode= ServletUtils.urlEncode(url);
            message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+oa_appId+"&redirect_type=jump&redirect_url="+encode;
        }
        oa.setMessageUrl(message_url);
        oa.setHead(head);
        oa.setBody(body);
        oa.setStatusBar(statusBar);
        msg.setOa(oa);
        msg.setMsgtype("oa");
        msg.setOa(oa);
        request.setMsg(msg);

        try{
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }

    /**
     * 发送给黄惠敏 迪卡侬项目组金额+部门基金
     */

    public void sendOemYwPrice(List<SubmitXftDataDto> arr, String userName){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        /**
         * 黄惠敏 0117280634108611247
         * 刘庆峰 2363192520970306
         * 戴燕云 275964113925083024
         * 张爱君 063241346224330026
         */
        request.setUseridList("0117280634108611247,275964113925083024,063241346224330026,2363192520970306");//黄惠敏,戴燕云
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();

        OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
        head.setBgcolor("FFFF6A00");
        head.setText("迪卡侬项目组，oem部门基金");
        OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
        List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<>();

         for (ImportXftDataDto dto : arr) {
             OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
             form1.setKey(dto.getUserName()+":");
             form1.setValue(dto.getUserPrice().doubleValue()+" 元");
             list.add(form1);
         }
        OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form1.setKey("发送时间:");
        form1.setValue(DateKit.getStringNowTime());
        list.add(form1);

        body.setForm(list);
//        body.setRich(rich);

//        Date now= DateKit.getLastMonthBegin();
        String month =arr.get(0).getAddDate();
        body.setTitle(month+"结算 oem汇总");



        OapiMessageCorpconversationAsyncsendV2Request.StatusBar statusBar = new OapiMessageCorpconversationAsyncsendV2Request.StatusBar();
        statusBar.setStatusBg("0xFFFF6A00");
        statusBar.setStatusValue("创建人:"+userName);
        String url="http://220.161.77.66:10111/#/h5Login?redirect=/ms/mouldList";
        String encode= ServletUtils.urlEncode(url);

        String message_url2="dingtalk://dingtalkclient/page/link?url="+encode+"&pc_slide=true";

        oa.setHead(head);
        oa.setBody(body);
        oa.setStatusBar(statusBar);
        msg.setOa(oa);
        msg.setMsgtype("oa");
        msg.setOa(oa);
        request.setMsg(msg);

        String accessToken = accessTokenH5Service.getAccessToken();
        try{
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }



    /**
     * 车间考勤待核对通知
     */
    public void kaWaitCheck(String addDate, KqDept dept,Integer num)  {
        if(UtilValidate.isEmpty(dept)){
            return;
        }
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(oa_agentId);
        request.setUseridList(dept.getGroupUserId());
        request.setToAllUser(false);
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();
        OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
        head.setBgcolor("FFFF6A00");
        head.setText("请尽快处理");
        OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
        List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<>();
        OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form1.setKey("日期:");
        form1.setValue(addDate);
        OapiMessageCorpconversationAsyncsendV2Request.Form form2 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form2.setKey("人数:");
        form2.setValue(num+"");
//        OapiMessageCorpconversationAsyncsendV2Request.Rich rich = new OapiMessageCorpconversationAsyncsendV2Request.Rich();
//        rich.setNum("200");
//        rich.setUnit("元");
        list.add(form1);
        list.add(form2);
        body.setForm(list);
//        body.setRich(rich);
        body.setTitle("考勤待确认通知");
//        body.setImage("@lADOADmaWMzazQKA");
//        body.setFileCount("3");
//        body.setAuthor("小钉");
        OapiMessageCorpconversationAsyncsendV2Request.StatusBar statusBar = new OapiMessageCorpconversationAsyncsendV2Request.StatusBar();
        statusBar.setStatusBg("0xFFFF6A00");
        statusBar.setStatusValue("进行中");
        String url="http://220.161.77.66:10111/oa_app/#/";
        String encode= ServletUtils.urlEncode(url);
        String message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+oa_corpid+"&container_type=work_platform&app_id="+oa_appId+"&redirect_type=jump&redirect_url="+encode;//&redirect_url=/h5Login
        //http://220.161.77.66:10111/#/h5Login
        oa.setMessageUrl(message_url);
        oa.setHead(head);
        oa.setBody(body);
        oa.setStatusBar(statusBar);
        msg.setOa(oa);
        msg.setMsgtype("oa");
        msg.setOa(oa);
        request.setMsg(msg);
        String accessToken = accessTokenOaAppService.getAccessToken();
        try{
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }


}
